/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#include <kmhdfs/common/big_endian.h>
#include <kmhdfs/common/exception.h>
#include <kmhdfs/common/exception_internal.h>
#include <kmhdfs/client/packet_header.h>

namespace Hdfs {
    namespace Internal {
        int PacketHeader::PkgHeaderSize = PacketHeader::CalcPkgHeaderSize();

        int PacketHeader::CalcPkgHeaderSize() {
            PacketHeaderProto header;
            header.set_offsetinblock(0);
            header.set_datalen(0);
            header.set_lastpacketinblock(false);
            header.set_seqno(0);
            return header.ByteSizeLong() + sizeof(int32_t) /*packet length*/ + sizeof(int16_t)/* proto length */;
        }

        int PacketHeader::GetPkgHeaderSize() {
            return PkgHeaderSize;
        }

        PacketHeader::PacketHeader() : packetLen(0) {
        }

        PacketHeader::PacketHeader(int packetLen, int64_t offsetInBlock, int64_t seqno,
                                   bool lastPacketInBlock, int dataLen) : packetLen(packetLen) {
            proto.set_offsetinblock(offsetInBlock);
            proto.set_seqno(seqno);
            proto.set_lastpacketinblock(lastPacketInBlock);
            proto.set_datalen(dataLen);
        }

        int PacketHeader::getDataLen() {
            return proto.datalen();
        }

        bool PacketHeader::isLastPacketInBlock() {
            return proto.lastpacketinblock();
        }

        bool PacketHeader::sanityCheck(int64_t lastSeqNo) {
            // We should only have a non-positive data length for the last packet
            if (proto.datalen() <= 0 && !proto.lastpacketinblock())
                return false;

            // The last packet should not contain data
            if (proto.lastpacketinblock() && proto.datalen() != 0)
                return false;

            // Seqnos should always increase by 1 with each packet received
            if (proto.seqno() != lastSeqNo + 1)
                return false;

            return true;
        }

        int64_t PacketHeader::getSeqno() {
            return proto.seqno();
        }

        int64_t PacketHeader::getOffsetInBlock() {
            return proto.offsetinblock();
        }

        int PacketHeader::getPacketLen() {
            return packetLen;
        }

        void PacketHeader::readFields(const char *buf, size_t size) {
            int16_t protoLen;
            assert(size > sizeof(packetLen) + sizeof(protoLen));
            packetLen = ReadBigEndian32FromArray(buf);
            protoLen = ReadBigEndian16FromArray(buf + sizeof(packetLen));

            if (packetLen < static_cast<int>(sizeof(int32_t)) || protoLen < 0
                || static_cast<int>(sizeof(packetLen) + sizeof(protoLen)) + protoLen > static_cast<int>(size)) {
                THROW(HdfsIOException, "Invalid PacketHeader, packetLen is %d, protoLen is %hd, buf size is %zu",
                      packetLen,
                      protoLen, size);
            }

            if (!proto.ParseFromArray(buf + sizeof(packetLen) + sizeof(protoLen),
                                      protoLen)) {
                THROW(HdfsIOException,
                      "PacketHeader cannot parse PacketHeaderProto from datanode response.");
            }
        }

        void PacketHeader::writeInBuffer(char *buf, size_t size) {
            buf = WriteBigEndian32ToArray(packetLen, buf);
            buf = WriteBigEndian16ToArray(proto.ByteSizeLong(), buf);
            proto.SerializeToArray(buf, size - sizeof(int32_t) - sizeof(int16_t));
        }
    }
}
